package com.alm.token.ui.activity;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupMenu;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alm.token.ConStant;
import com.alm.token.GlideApp;
import com.alm.token.MyApp;
import com.alm.token.R;
import com.alm.token.base.BaseActivity;
import com.alm.token.ui.data.BaseDialogBean;
import com.alm.token.ui.data.bean.CurrencyChangeData;
import com.alm.token.ui.data.bean.CurrencyChangeIndexData;
import com.alm.token.ui.data.bean.CurrencyChangeListData;
import com.alm.token.ui.data.bean.DrawIndexData;
import com.alm.token.ui.data.bean.MiningWalletTypeIndexData;
import com.alm.token.ui.data.bean.RegisterData;
import com.alm.token.ui.data.bean.StakingMiningMoneyDrawIndexData;
import com.alm.token.ui.data.bean.UserWalletData;
import com.alm.token.ui.fragmentDialog.NoticeDialogFragment;
import com.alm.token.ui.fragmentDialog.ToSetPayPswDialogFragment;
import com.alm.token.ui.persenter.MyPreenter;
import com.alm.token.ui.view.CurrencyChangeView;
import com.alm.token.ui.viewpagefragment.BannerOneFragment;
import com.alm.token.utils.ActionBarHelper;
import com.alm.token.utils.CommonUtil;
import com.alm.token.utils.EmptyUtils;
import com.alm.token.utils.LogUtil;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import baibeiwallet.Constant;
import baibeiwallet.event.getUserWalletEvent;
import baibeiwallet.utils.DialogUtils;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TransformActivity extends BaseActivity implements TextWatcher, CurrencyChangeView<Object> {
    @BindView(R.id.rightItem)
    TextView rightItem;
    @BindView(R.id.img_bizhong1icon)
    ImageView imgBizhong1icon;
    @BindView(R.id.tv_bizhong1tv)
    TextView tvBizhong1tv;
    //    @BindView(R.id.ly_moneytyep1)
//    LinearLayout lyMoneytyep1;
//    @BindView(R.id.img_clickchange)
//    ImageView imgClickchange;
    @BindView(R.id.img_bizhong2icon)
    ImageView imgBizhong2icon;
    @BindView(R.id.tv_bizhong2tv)
    TextView tvBizhong2tv;
    @BindView(R.id.tv_moneymax)
    TextView tvMoneyMax;

    //    @BindView(R.id.ly_moneytyep2)
//    LinearLayout lyMoneytyep2;
    @BindView(R.id.edt_inputnumber)
    EditText edtInputnumber;

    @BindView(R.id.edt_mathnumber)
    EditText edtMathnumber;


    //    @BindView(R.id.tv_bizhongshow)
//    TextView tvBizhongshow;
    @BindView(R.id.tv_mathmoney)
    TextView tvMathmoney;
    @BindView(R.id.tv_outmoneycommit)
    TextView tvOutmoneycommit;
    @BindView(R.id.tv_jiaoyifeimath)
    TextView tvJiaoyifeiMath;
    @BindView(R.id.tv_jiaoyifeimath1)
    TextView tvJiaoyifeiMath1;
    @BindView(R.id.tv_zhuanhuanmsg)
    TextView tvZhuanhuanMsg;

    @BindView(R.id.img_changebizhong)
    ImageView imgChangebizhong;
    @BindView(R.id.tv_bizhong1price)
    TextView tvBizhong1Price;
    @BindView(R.id.tv_bizhong2price)
    TextView tvBizhong2Price;
    @BindView(R.id.tv_bizhong1money)
    TextView tvBizhong1Money;
    @BindView(R.id.tv_bizhong2money)
    TextView tvBizhong2Money;
    @BindView(R.id.tv_showStr)
    TextView tvShowStr;

    public List<CurrencyChangeIndexData.DataBean.WalletsBean> walletsBeans = new ArrayList<>();
    private double number = 0d;
    private int bizhongType1 = 0, bizhongType2 = 0;
    private CurrencyChangeIndexData.DataBean currencyChangeIndexDataBean;
    CurrencyChangeIndexData.DataBean.WalletsBean walletsBean;
    CurrencyChangeIndexData.DataBean.WalletsBean.ChangeListBean changeListBean;
    List<CurrencyChangeIndexData.DataBean.WalletsBean.ChangeListBean> changeList = new ArrayList<>();
    private double pro = 0.00000d;
    private final int WALLETBEANTYPE = 1, CHANGEBEANTYPE = 2;
    public final static int EXCHANGEQUOTA_TYPE = 11;

    private int intentType = 0;

    @Override
    protected MyPreenter initPreenter() {
        intentType=getIntent().getIntExtra("intentType",0);
        return new MyPreenter(this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_transform;
    }

    @Override
    protected void initVariables() {
        if (EmptyUtils.isNotEmpty(currencyChangeIndexDataBean)) {
//            currencyChangeIndexDataBean = MyApp.getCurrencyChangeIndexDataBean();
            if (EmptyUtils.isNotEmpty(currencyChangeIndexDataBean) && EmptyUtils.isNotEmpty(currencyChangeIndexDataBean.getWallets())) {
                walletsBeans.addAll(currencyChangeIndexDataBean.getWallets());
                for (int i = 0; i < walletsBeans.size(); i++) {
                    if (EmptyUtils.isNotEmpty(walletsBeans.get(i).getChangeList())) {
                        bizhongType1 = i;
                        break;
                    }
                }
            }
        }
    }

    @Override
    protected void initViews() {
        new ActionBarHelper(this).setTitle(getString(R.string.transform_hint_title1), getString(R.string.transform_hint_title2)).setRightText(getString(R.string.title_hint_item13));
        changView();
    }

    private void changView() {
        if (EmptyUtils.isNotEmpty(currencyChangeIndexDataBean)) {
            tvZhuanhuanMsg.setText(!currencyChangeIndexDataBean.getMsg().isEmpty() ? currencyChangeIndexDataBean.getMsg() : getString(R.string.conversioncurrency_hint1_warn2));
            pro = Double.parseDouble(currencyChangeIndexDataBean.getPro());

            tvZhuanhuanMsg.setText(!currencyChangeIndexDataBean.getMsg().isEmpty() ? currencyChangeIndexDataBean.getMsg() : getString(R.string.conversioncurrency_hint1_warn2));
            pro = Double.parseDouble(currencyChangeIndexDataBean.getPro());
            if (EmptyUtils.isNotEmpty(walletsBeans)) {
                walletsBean = walletsBeans.get(bizhongType1);
                changeList = walletsBean.getChangeList();
                changeListBean = changeList.get(bizhongType2);

                GlideApp.with(this).load(walletsBean.getLogo())
                        .listener(new RequestListener<Drawable>() {
                            @Override
                            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                                return false;
                            }

                            @Override
                            public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
//                            if (EmptyUtils.isNotEmpty(resource)) {
//                                CommonUtil. changeSVGColor(TransformActivity.this, imgBizhong1icon, resource, R.color.black);
//                                return true;
//                            }
                                return false;
                            }
                        }).error(R.drawable.icon_arctic).into(imgBizhong1icon);
                GlideApp.with(this).load(changeListBean.getLogoX())
                        .listener(new RequestListener<Drawable>() {
                            @Override
                            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                                return false;
                            }

                            @Override
                            public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
//                            if (EmptyUtils.isNotEmpty(resource)) {
//                                CommonUtil. changeSVGColor(TransformActivity.this, imgBizhong2icon, resource, R.color.black);
//                                return true;
//                            }
                                return false;
                            }
                        }).error(R.drawable.icon_arctic).into(imgBizhong2icon);

                tvBizhong1tv.setText(walletsBean.getName());
                tvBizhong2tv.setText(changeListBean.getNameX());

                edtInputnumber.addTextChangedListener(this);
                number = 0d;
                edtInputnumber.setText("");
                mathNumber(number);
            }
        }
    }

    double money = 0.0000d;

    public void mathNumber(double number) {


        tvShowStr.setText(changeListBean.getExchangeQuotaSwitch()==1?
                getString( R.string.mine_hint_myed)+" =":getString( R.string.alphago_hint_usable));
        tvBizhong1Money.setText(changeListBean.getExchangeQuotaSwitch()==1?
               "$"+ CommonUtil.saveToPoint4WithNo(changeListBean.getExchangeQuota()):CommonUtil.saveToPoint4WithNo(walletsBean.getMoney()) + " " + walletsBean.getName());
        tvBizhong2Money.setText(getString(R.string.currency_hint_balance, CommonUtil.saveToPoint4WithNo(changeListBean.getMoneyX())));
        tvBizhong1Price.setText(getString(R.string.transform_hint_exchangerate)
                + ("\t1 " + walletsBean.getName()
                + " ≈ "
                + CommonUtil.saveToPoint4WithNo(changeListBean.getPriceX() != 0 ? walletsBean.getPrice() / changeListBean.getPriceX() : 0)
                + " " + changeListBean.getNameX()));

//        tvBizhong1Price.setText(dec1.format(walletsBeans.get(bizhongType1).getPrice()) + "USD");
//        tvBizhong2Price.setText(dec1.format(walletsBeans.get(bizhongType2).getPrice()) + "USD");

        money = number * walletsBean.getPrice();
//        tvMathmoney.setText("≈" + dec1.format(money) + "USD/" + walletsBeans.get(bizhongType1).getName());
        tvMathmoney.setText("≈$" + CommonUtil.saveToPoint2WithNo(money));
//        tvJiaoyifeiMath.setText(pro + walletsBeans.get(bizhongType1).getName()
//                + "≈" + dec1.format(money * pro/walletsBeans.get(bizhongType2).getPrice())+walletsBeans.get(bizhongType2).getName());
        tvJiaoyifeiMath.setText("≈" + CommonUtil.saveToPoint8WithNo(money * pro / changeListBean.getPriceX()) + changeListBean.getNameX());

        tvJiaoyifeiMath1.setText(":" + CommonUtil.saveToPoint8WithNo(money * pro / changeListBean.getPriceX()) + changeListBean.getNameX());


        edtMathnumber.setText(CommonUtil.saveToPoint4WithNo(money / changeListBean.getPriceX()));
//        edtMathnumber.setText(dec1.format((money - money * pro) / walletsBeans.get(bizhongType2).getPrice()));
    }

    @Override
    protected void loadData() {
        if (EmptyUtils.isEmpty(currencyChangeIndexDataBean)) {
            if(intentType==EXCHANGEQUOTA_TYPE){
                preenter.getCurrencyChangeIndexData("2","12");
            }else {
                preenter.getCurrencyChangeIndexData();
            }

        }
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.rightItem, R.id.tv_outmoneycommit, R.id.tv_moneymax, R.id.img_changebizhong,
            R.id.ly_moneytyep1, R.id.ly_moneytyep2})
    public void onViewClicked(View view) {
        if (view.getId() != R.id.rightItem && walletsBeans.size() == 0)
            return;
        if (EmptyUtils.isEmpty(currencyChangeIndexDataBean))
            return;
        switch (view.getId()) {

            case R.id.tv_moneymax:
                if (walletsBean.getMoney() > 10000d) {
                    number = 10000d;
                } else {
                    number = walletsBean.getMoney();
                }
                edtInputnumber.setText((int) number + "");
                mathNumber(number);
                break;

            case R.id.rightItem:
//                showToast("lishi");
                Intent intent = new Intent(this, VipOrderListActivity.class);
                intent.putExtra("intentType", 1);
                startActivity(intent);
//                preenter.getCurrencyChangeListData("100","1");
                break;
            case R.id.ly_moneytyep1:
                if (EmptyUtils.isNotEmpty(walletsBeans)) {
                    if (EmptyUtils.isNotEmpty(popWindow)) {
                        popWindow.dismiss();
                        popWindow = null;
                    }
                    showPopwindow(WALLETBEANTYPE);
                }

                break;
            case R.id.ly_moneytyep2:
                if (EmptyUtils.isNotEmpty(changeList)) {
                    if (EmptyUtils.isNotEmpty(popWindow)) {
                        popWindow.dismiss();
                        popWindow = null;
                    }
                    showPopwindow(CHANGEBEANTYPE);
                }
                break;
            case R.id.img_changebizhong:
//                int type = bizhongType1;
//                bizhongType1 = bizhongType2;
//                bizhongType2 = type;
//                GlideApp.with(this).load(walletsBeans.get(bizhongType1).getLogo())
//                        .listener(new RequestListener<Drawable>() {
//                            @Override
//                            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
//                                return false;
//                            }
//
//                            @Override
//                            public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
////                                if (EmptyUtils.isNotEmpty(resource)) {
////                                    CommonUtil. changeSVGColor(TransformActivity.this, imgBizhong1icon, resource, R.color.black);
////                                    return true;
////                                }
//                                return false;
//                            }
//                        }).error(R.drawable.icon_arctic).into(imgBizhong1icon);
//                GlideApp.with(this).load(walletsBeans.get(bizhongType2).getLogo())
//                        .listener(new RequestListener<Drawable>() {
//                            @Override
//                            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
//                                return false;
//                            }
//
//                            @Override
//                            public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
////                                if (EmptyUtils.isNotEmpty(resource)) {
////                                    CommonUtil. changeSVGColor(TransformActivity.this, imgBizhong2icon, resource, R.color.black);
////                                    return true;
////                                }
//                                return false;
//                            }
//                        }).error(R.drawable.icon_arctic).into(imgBizhong2icon);
//                tvBizhong1tv.setText(walletsBeans.get(bizhongType1).getName());
//                tvBizhong2tv.setText(walletsBeans.get(bizhongType2).getName());
//                mathNumber(number);
                break;
            case R.id.tv_outmoneycommit:
                if (number == 0) {
                    showToast(getString(R.string.conversioncurrency_hint1_warn3));
                    break;
                }

//                if (walletsBeans != null && walletsBeans.size() != 0 && changeListBean.isChangeX()) {//可兑换
                UserWalletData.DataBean.SysMsgBean sysMsgBean = new UserWalletData.DataBean.SysMsgBean();
                sysMsgBean.setTitle(getString(R.string.conversioncurrency_hint1_warn4));
                sysMsgBean.setContent(
                        getString(R.string.conversioncurrency_hint1_warn8, CommonUtil.saveToPoint4WithNo(number),
                                "" + tvBizhong1tv.getText().toString(), tvBizhong2tv.getText().toString()));
                DialogUtils.showInfor1(this, sysMsgBean, (pwd) -> {

                    DialogUtils.enterPayPsw(this, (pwd1) -> {

                        //eth1 mio2
                        preenter.getCurrencyChangeData(number + "", walletsBean.getId() + "", changeListBean.getIdX() + "", pwd1);
                    });
                });
//                } else {
//                    showToast(getString(R.string.conversioncurrency_hint1_warn5));
//                }
//                showToast("money="+number+"  type1="+bizhongType1+"  type2="+bizhongType2);


                break;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence s, int i, int i1, int i2) {

        if (s.toString().contains(".")) {
            if (s.length() - 1 - s.toString().indexOf(".") > 4) {
                s = s.toString().subSequence(0, s.toString().indexOf(".") + 5);
                edtInputnumber.setText(s);
                edtInputnumber.setSelection(s.length());
            }
        }
        if (s.toString().trim().substring(0).equals(".")) {
            s = "0" + s;
            edtInputnumber.setText(s);
            edtInputnumber.setSelection(2);
        }

        if (s.toString().startsWith("0")
                && s.toString().trim().length() > 1) {
            if (!s.toString().substring(1, 2).equals(".")) {
                edtInputnumber.setText(s.subSequence(0, 1));
                edtInputnumber.setSelection(1);
                return;
            }
        }

        number = Double.parseDouble(s != null ? (!s.toString().isEmpty() ? s.toString() : "0") : "0");

        if (number > walletsBean.getMoney()) {
            showToast(getString(R.string.conversioncurrency_hint1_warn6));
//            if (walletsBeans.get(bizhongType1).getMoney() > 10000d) {
//                number = 10000d;
//            } else {
//                number = walletsBeans.get(bizhongType1).getMoney();
//            }
            edtInputnumber.setText("");
            return;
        }
        mathNumber(number);
    }

    @Override
    public void afterTextChanged(Editable s) {
//        showToast(s != null ? s.toString() : "");

    }


    @Override
    public void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onUserWalletEvent1(getUserWalletEvent userWalletEvent) {

    }

    @Override
    public void getCurrencyChangeData(CurrencyChangeData currencyChangeData) {
        showToast(currencyChangeData.getMsg());
        if (currencyChangeData.getStatus() == 0) {
            EventBus.getDefault().post(new getUserWalletEvent(null));
//            preenter.getCurrencyChangeIndexData();
            finish();

        } else if (currencyChangeData.getStatus() == 3) {//需要登录


        }

    }

    @Override
    public void getCurrencyChangeListData(CurrencyChangeListData currencyChangeListData) {
        if (currencyChangeListData.getStatus() == 0) {
            showToast(currencyChangeListData.getMsg());
//            finish();
        }
    }

    @Override
    public void getCurrencyChangeIndexData(CurrencyChangeIndexData currencyChangeIndexData) {
//        if (currencyChangeIndexData != null && currencyChangeIndexData.getStatus() == 0) {
////            showToast(currencyChangeIndexData.getMsg());
//            MyApp.setCurrencyChangeIndexDataBean(currencyChangeIndexData.getData());
//        }
//        finish();
        if (currencyChangeIndexData.getStatus() == 0) {
            if (EmptyUtils.isNotEmpty(currencyChangeIndexData.getData())) {
                currencyChangeIndexDataBean = currencyChangeIndexData.getData();
                initVariables();
                changView();
            }
        } else {
            showToast(currencyChangeIndexData.getMsg());
        }
    }

    @Override
    public void getDrawIndexData(DrawIndexData drawIndexData) {

    }

    @Override
    public void getDrawCandyData(RegisterData drawCandyData) {

    }

    @Override
    public void getMiningWalletTypeIndexData(MiningWalletTypeIndexData miningWalletTypeIndexData) {

    }

    @Override
    public void getData(Object o) {

    }

    @Override
    public void jumpToLoginAct(int staue) {
        if (staue == 3) {
//            showToast(getString(R.string.common_relogin));
//            Intent intent = new Intent();
//            intent.setAction(ConStant.EXIT_APP);
//            sendBroadcast(intent);
//            startActivity(BlockWalletLoginActivity.class);
//            finish();
        } else if (staue == 4) {
            showToSetPaypswDialog();
        } else if (staue == 666) {
            showToUpdata();
        }
    }

    @Override
    public void onError(String error) {
        showToast(getString(R.string.conversioncurrency_hint1_warn7));
    }

    @Override
    public void ShowProgress() {

        showWaitDialog();
    }

    @Override
    public void hideProgress() {
        hideWaitDialog();

    }


    PopupWindow popWindow;

    private void showPopwindow(int type) {
        View parent = ((ViewGroup) this.findViewById(android.R.id.content)).getChildAt(0);
        View popView = View.inflate(this, R.layout.view_pop_selecter, null);
        LinearLayout ly_pop_body = popView.findViewById(R.id.ly_pop_body);

        if (type == WALLETBEANTYPE) {
            for (int i = 0; i < walletsBeans.size(); i++) {
                CurrencyChangeIndexData.DataBean.WalletsBean coinBean = walletsBeans.get(i);

                View itemView = View.inflate(this, R.layout.item_pop_wallet, null);
                GlideApp.with(this)
                        .load(coinBean.getLogo())
                        .error(R.drawable.icon_arctic)
                        .into((ImageView) itemView.findViewById(R.id.img_bizhongicon));
                ((TextView) itemView.findViewById(R.id.name)).setText(coinBean.getName());
                ((TextView) itemView.findViewById(R.id.desc)).setVisibility(View.INVISIBLE);

                ((TextView) itemView.findViewById(R.id.money1)).setText(CommonUtil.saveToPoint4WithNo(coinBean.getMoney()));
                ((TextView) itemView.findViewById(R.id.money2)).setVisibility(View.INVISIBLE);

                int finalI = i;
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (EmptyUtils.isNotEmpty(walletsBeans.get(finalI).getChangeList())) {
                            bizhongType1 = finalI;
                            bizhongType2 = 0;
                            changView();
                        }
                        popWindow.dismiss();
                    }
                });
                ly_pop_body.addView(itemView);

            }
        } else {
            for (int i = 0; i < changeList.size(); i++) {
                CurrencyChangeIndexData.DataBean.WalletsBean.ChangeListBean coinBean = changeList.get(i);

                View itemView = View.inflate(this, R.layout.item_pop_wallet, null);
                GlideApp.with(this)
                        .load(coinBean.getLogoX())
                        .error(R.drawable.icon_arctic)
                        .into((ImageView) itemView.findViewById(R.id.img_bizhongicon));
                ((TextView) itemView.findViewById(R.id.name)).setText(coinBean.getNameX());
                ((TextView) itemView.findViewById(R.id.desc)).setVisibility(View.INVISIBLE);

                ((TextView) itemView.findViewById(R.id.money1)).setText(CommonUtil.saveToPoint4WithNo(coinBean.getMoneyX()));
                ((TextView) itemView.findViewById(R.id.money2)).setVisibility(View.INVISIBLE);

                int finalI = i;
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        bizhongType2 = finalI;
                        changView();
                        popWindow.dismiss();
                    }
                });
                ly_pop_body.addView(itemView);

            }
        }

        ly_pop_body.setBackgroundColor(getResources().getColor(R.color.text_cccccc));


        RelativeLayout outside = popView.findViewById(R.id.pop_outside);

        int width = getResources().getDisplayMetrics().widthPixels;
        int height = getResources().getDisplayMetrics().heightPixels;

        if (popWindow == null) {
            popWindow = new PopupWindow(popView, width, height);
            popWindow.setAnimationStyle(R.style.BottomToTopAnim);
//        popWindow.setFocusable(true);
            popWindow.setOutsideTouchable(true);// 设置同意在外点击消失
        }
        outside.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
            }
        });

        ColorDrawable dw = new ColorDrawable(0x30000000);
        popWindow.setBackgroundDrawable(dw);
        popWindow.showAtLocation(parent, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }
}
